package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.youdianda.common.CustomException;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.dto.*;
import com.itheima.youdianda.entity.*;
import com.itheima.youdianda.service.*;
import com.itheima.youdianda.util.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
@Slf4j
public class IndexController {

    @Autowired
    private YddCateService yddCateService;
    @Autowired
    private YddLinkService yddLinkService;
    @Autowired
    private YddTagService yddTagService;
    @Autowired
    private YddAdvposService yddAdvposService;
    @Autowired
    private YddAdvimgService yddAdvimgService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private YddArticleService yddArticleService;
    @Autowired
    private YddUserService yddUserService;

    @Autowired
    private IndexService indexService;

    @GetMapping("/index")
    public R<IndexDto> index(){
        log.info("111111111111");
        // 创建一个空的indexDto对象
        IndexDto indexDto = new IndexDto();
        // 获取所有cate数据
        List<YddCate> cateList = yddCateService.list();
        indexDto.setAllCate(cateList);
        // 获取link数据
        List<YddLink> linkList = yddLinkService.list();
        indexDto.setAllLink(linkList);
        // 获取所有tag数据
        List<YddTag> tagList = yddTagService.list();
        indexDto.setAllTag(tagList);
        // 获取banner数据
        List<YddAdvimg> banner = yddAdvimgService.getBanner();
        indexDto.setBanner(banner);
        // 获取config数据
        ConfigDto config = sysConfigService.getConfig();
        indexDto.setConfig(config);
        // 获取showBanner数据
        List<YddAdvimg> showBanner = yddAdvimgService.getShowBanner();
        indexDto.setShowBanner(showBanner);
        // 获取sideBanner数据
        List<YddAdvimg> sideBanner = yddAdvimgService.getSideBanner();
        indexDto.setSideBanner(sideBanner);
        return R.success(indexDto);
    }

    @GetMapping("/hot")
    public R<HotDto> getHot(){
        HotDto hot = yddArticleService.getHot();
        return R.success(hot);
    }

    @GetMapping("/recommend")
    public R<Recommend> getRecommend(){
        List<YddArticle> yddArticles = yddArticleService.list();
        List<ArticleDto> articleDtos = new ArrayList<>();
        for (YddArticle yddArticle : yddArticles) {
            Integer cateid = yddArticle.getCateid();
            if (cateid==3||cateid==4||cateid==9){
                YddCate cate = yddCateService.getById(cateid);
                ArticleDto articleDto = new ArticleDto();
                articleDto.setCatename(cate.getCatename());
                articleDto.setClick(yddArticle.getClick());
                articleDto.setId(yddArticle.getId());
                articleDto.setLovenum(yddArticle.getLovenum());
                articleDto.setPic(yddArticle.getPic());
                articleDto.setTitle(yddArticle.getTitle());
                articleDtos.add(articleDto);
            }
        }
        Recommend recommend = new Recommend();
        recommend.setList(articleDtos);
        recommend.setDesc("侧边推荐文章");
        return R.success(recommend);
    }

    @GetMapping("/show")
    public R<Map<String,Object>> show(@RequestParam String id){
        log.info("/index/show,id:{}",id);
        Map<String,Object> map = yddArticleService.show(id);
        return R.success(map);
    }

    @GetMapping("/new")
    public R<NewDto> getNew(String page,String limit){
        NewDto newDto = new NewDto();
        NewListDto newList = yddArticleService.getNewList(page, limit);
        newDto.setList(newList);
        newDto.setDesc("首页大列表");
        // num_day
        long currentTime = System.currentTimeMillis();
        Long dayTime = currentTime - 3600*1000*24;
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(YddArticle::getCreate_date,currentTime);
        wrapper.gt(YddArticle::getCreate_date,dayTime);
        int num_day = yddArticleService.count(wrapper);
        newDto.setNum_day(num_day);
        // num_week
        Long weekTime = currentTime - 3600*1000*24*7;
        LambdaQueryWrapper<YddArticle> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.lt(YddArticle::getCreate_date,currentTime);
        wrapper2.gt(YddArticle::getCreate_date,weekTime);
        int num_week = yddArticleService.count(wrapper);
        newDto.setNum_day(num_week);
        return R.success(newDto);
    }

    @GetMapping("/list")
    public R<Map<String,DataListDto>> getList(String page,String limit,String cateid){
        DataListDto dataList = yddArticleService.getDataList(page, limit, cateid);
        Map<String,DataListDto> map = new HashMap<>();
        map.put("list",dataList);
        return R.success(map);
    }

    @PostMapping("/reg")
    public R<Map<String,String>> register(@RequestBody YddUser yddUser){
        String mobile = yddUser.getMobile();
        String password = yddUser.getPassword();
        // 首先判断手机号是否存在
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getMobile,mobile);
        YddUser one = yddUserService.getOne(wrapper);
        if (one!=null){
            throw new CustomException("当前手机号已被注册");
        }
        // 否则将密码进行加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        // 存储数据
        YddUser newYddUser = new YddUser();
        newYddUser.setPassword(password);
        newYddUser.setMobile(mobile);
        newYddUser.setUsername(mobile);
        yddUserService.save(newYddUser);
        // 生成token
        YddUser newUser = yddUserService.getOne(wrapper);
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("id", newUser.getId());
        String token = AppJwtUtil.getToken(tokenMap);
        HashMap<String, String> map = new HashMap<>();
        map.put("token", token);
        return R.success(map);
    }
}
